package com.chen.Dome3UDP2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

public class UDPClientDome1 {
    public static void main(String[] args) throws Exception {
        //目标：完成UDP通信的客户端，多发多收
        //1.创建发送端的对象，并且发送数据
        System.out.println("发送端已经启动...");
        Scanner sc=new Scanner(System.in);
        DatagramSocket ds = new DatagramSocket();//创建发送端的对象,生成随机端口号
        while (true) {
            System.out.println("请输入要发送的数据:");
            String s=sc.nextLine();
            if(s.equals("exit")) {
                System.out.println("发送端已经退出...");
                ds.close();
                break;
            }
            byte[] by =s.getBytes();
            DatagramPacket dp = new DatagramPacket(by, by.length, InetAddress.getByName("127.0.0.1"), 10000);
            ds.send(dp);
        }


    }
}
